﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DreamBox.Admin.Posts
{
    public partial class Topic : BLL.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DAL.PSelect ps = new DAL.PSelect();
                DropProperty.DataSource = ps.Select("dbox_Property","ProID,ProName","IsEnabled=1");
                DropProperty.DataTextField = "ProName";
                DropProperty.DataValueField = "ProID";
                DropProperty.DataBind();
                ViewState["PageWhere"] = "";
            }
            BindList(ViewState["PageWhere"].ToString());
        }
        void BindList(string where)
        {
            PagerList.TableName = "dbox_v_posts";
            PagerList.FldName = "PostsID";
            PagerList.PagerControls = RepeaterList;
            PagerList.PageSize = 10;
            PagerList.Where = where;
            PagerList.Order = 1;
        }
        //删除文字
        protected void LinkBtnDelete_Click(object sender, EventArgs e)
        {
            DAL.Posts.Posts topic = new DAL.Posts.Posts();
            CheckBox box = null;
            foreach (RepeaterItem item in RepeaterList.Items)
            {
                box = item.FindControl("CheckBoxSelect") as CheckBox;
                if (box != null&&box.Checked)
                {
                    topic.Delete(int.Parse(box.ToolTip), 0);
                }
            }
            BLL.Admin.Tips.DetailedTips(this, "操作成功!", "/Posts/Topic.aspx", "1");
        }
        //按照时间段查询
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            if (txtStartTime.Text.Trim() != "" && txtEndTime.Text.Trim() != "")
            {
                string sTime = txtStartTime.Text.Trim() + " 00:00:01";
                string endTime = txtEndTime.Text.Trim() + " 23:59:59";
                if (DateTime.Parse(sTime) < DateTime.Parse(endTime))
                {
                    ViewState["PageWhere"] = " PostDate>='" + sTime + "' and PostDate<='" + endTime + "'";
                    PagerList.Where = ViewState["PageWhere"].ToString();
                    PagerList.BindPage(1);
                }
            }
        }
        //按用户名查询
        protected void btnSelectUserName_Click(object sender, EventArgs e)
        {
            string UserName = txtUserName.Text.Trim();
            if (UserName != "")
            {
                ViewState["PageWhere"] = "UserName='" + UserName + "'";
            }
            else
            {
                ViewState["PageWhere"] = "";
            }
            PagerList.Where = ViewState["PageWhere"].ToString();
            PagerList.BindPage(1);
        }
        //显示所有
        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            ViewState["PageWhere"] = "";
            PagerList.Where = ViewState["PageWhere"].ToString();
            PagerList.BindPage(1);
        }
        //锁定贴子
        protected void LinkBtnIsStatusTrue_Click(object sender, EventArgs e)
        {
            UpdateStatus(false);
        }
        //取消锁定
        protected void LinkBtnIsStatusFalse_Click(object sender, EventArgs e)
        {
            UpdateStatus(true);
        }
        void UpdateStatus(bool IsStatus)
        {
            DAL.Posts.Posts topic = new DAL.Posts.Posts();
            CheckBox box = null;
            foreach (RepeaterItem item in RepeaterList.Items)
            {
                box = item.FindControl("CheckBoxSelect") as CheckBox;
                if (box != null && box.Checked)
                {
                    topic.UpdateStatus(int.Parse(box.ToolTip), IsStatus);
                }
            }
            BLL.Admin.Tips.JsTips(this, "操作成功");
            //
            int PeIndex = int.Parse((PagerList.FindControl("PeIndex") as Label).Text);

            PagerList.BindPage(PeIndex);
        }
        /// <summary>
        /// 更新鉴定属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkBtnProperty_Click(object sender, EventArgs e)
        {
            DAL.Posts.Posts topic = new DAL.Posts.Posts();
            CheckBox box = null;
            int Property=int.Parse(DropProperty.SelectedValue);
            foreach (RepeaterItem item in RepeaterList.Items)
            {
                box = item.FindControl("CheckBoxSelect") as CheckBox;
                if (box != null && box.Checked)
                {
                    topic.UpdateProperty(int.Parse(box.ToolTip), Property);
                }
            }
            BLL.Admin.Tips.JsTips(this,"操作成功");
        }
        /// <summary>
        /// 按ID查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnTID_Click(object sender, EventArgs e)
        {
            string TID = txtTID.Text.Trim();
            if (TID != "")
            {
                ViewState["PageWhere"] = "TID='" + TID + "'";
            }
            else
            {
                ViewState["PageWhere"] = "";
            }
            PagerList.Where = ViewState["PageWhere"].ToString();
            PagerList.BindPage(1);
        }
    }
}